package com.ruoyi.project.online_experimentTend.controller;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.RuoYiConfig;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.onlineExperimentOrder.domain.OnlineExperimentOrder;
import com.ruoyi.project.onlineExperimentOrder.service.OnlineExperimentOrderService;
import com.ruoyi.project.online_experiment.domain.OnlineExperiment;
import com.ruoyi.project.online_experiment.service.IOnlineExperimentService;
import com.ruoyi.project.system.user.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

/**
 * @ClassName JiaoshiOnlineExperimentController
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-04-15 13:11
 **/
@Controller
@RequestMapping("/system/OnlineExperimentTend")
public class JiaoshiOnlineExperimentController extends BaseController {

    private String prefix = "system/onlineExperimentTend";

    @Autowired
    private IOnlineExperimentService iOnlineExperimentService;

    @Autowired
    private OnlineExperimentOrderService onlineExperimentOrderService;

    @Autowired
    private IUserService iUserService;

    @RequiresPermissions("system:onlineExperimentTend:view")
    @GetMapping()
    public String experiment()
    {
        return prefix + "/onlineExperiment";
    }

    /**
     * 查询【请填写功能名称】列表
     */
    @RequiresPermissions("system:onlineExperimentTend:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OnlineExperiment onlineExperiment)
    {
        onlineExperiment.setAssignTeacher(getSysUser().getUserId());
        startPage();
        List<OnlineExperiment> list = iOnlineExperimentService.selectOnlineExperimentList(onlineExperiment);
        return getDataTable(list);
    }

    /**
     * 修改【请填写功能名称】
     */
    @GetMapping("/edit/{onlineExperimentId}")
    public String edit(@PathVariable("onlineExperimentId") Long onlineExperimentId, ModelMap mmap)
    {
        OnlineExperiment onlineExperiment = iOnlineExperimentService.selectOnlineExperimentById(onlineExperimentId);
        mmap.put("onlineExperiment", onlineExperiment);
        return prefix + "/edit";
    }

    /**
     * 修改基本信息保存
     */
   // @Log(title = "修改详细内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(MultipartFile file,OnlineExperiment onlineExperiment) throws IOException {
        if(file!=null){
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            //设置图片上传的路径
            onlineExperiment.setOnlineExperimentImg(fileName);
        }
        return toAjax(iOnlineExperimentService.updateOnlineExperiment(onlineExperiment));
        //return toAjax(true);
    }

    /**
     * 修改实验报告保存
     */
    @PostMapping("/edit1")
    @ResponseBody
    public AjaxResult editSave1(@RequestBody OnlineExperiment onlineExperiment)
    {
        return toAjax(iOnlineExperimentService.updateOnlineExperiment(onlineExperiment));
    }


    /**
     * 获取自己名下的所有实验预约信息
     * @return
     */
    @RequiresPermissions("system:onlineExperimentOrder2:view")
    @GetMapping("/getAllOrder")
    public String getAllExperimentOrder()
    {
        return prefix + "/onlineExperimentOrder";
    }


    /**
     * 查询【预约信息】列表
     */
    @RequiresPermissions("system:onlineExperimentOrder2:list")
    @PostMapping("/orderList")
    @ResponseBody
    public TableDataInfo list(OnlineExperimentOrder onlineExperimentOrder)
    {
        long userId = getSysUser().getUserId();
        onlineExperimentOrder.setTeacherId(userId);
        startPage();
        List<OnlineExperimentOrder> list = onlineExperimentOrderService.getOrderList(onlineExperimentOrder);
        return getDataTable(list);
    }

    /**
     * 教师审批
     * @param data
     * @return
     */
    @RequiresPermissions("system:onlineExperimentOrder2:edit")
    @PostMapping("/approve")
    @ResponseBody
    public AjaxResult approve(String data)
    {
        /**
         * 参数以,分割
         */
        String[] array=data.split(",");
        /**
         * 创建预约实体
         */
        OnlineExperimentOrder onlineExperimentOrder=new OnlineExperimentOrder();
        //配置内容
        onlineExperimentOrder.setOnlineExperimentId(Integer.valueOf(array[0]));
        onlineExperimentOrder.setUserId(Integer.valueOf(array[1]));
        onlineExperimentOrder.setStatus(Integer.valueOf(array[2]));
        onlineExperimentOrder.setUserName(iUserService.selectUserById(Long.valueOf(array[1])).getUserName());
        /**
         * 通过审批时先判断之前是否已经通过
         */
        OnlineExperimentOrder onlineExperimentOrder1=onlineExperimentOrderService.getOnlineExperimentOrder(onlineExperimentOrder);
        /**
         * 返回结果
         */
        if(StringUtils.isNull(onlineExperimentOrder1.getExperimentTeaId()) || onlineExperimentOrder1.getExperimentTeaId()==null){
            return toAjax(onlineExperimentOrderService.approve(onlineExperimentOrder));
        }else if(Integer.valueOf(array[2])==3){
            return toAjax(onlineExperimentOrderService.approve(onlineExperimentOrder));
        }else{
            return toAjax(true);
        }
    }
}
